package com.tydic.baselibrary.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.LayoutRes;
import androidx.fragment.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tydic.baselibrary.activity.BaseActivity;
import com.tydic.baselibrary.dialog.LoadingDialog;

/**
 * @author zouyuanfeng
 * @version 16-11-18 10:07
 */

public abstract class BaseFragment extends Fragment {
    protected Toast mToast = null;
    protected Handler mHandler = new ActivityHandler(this);
    protected SharedPreferences sp;
    protected BaseActivity mActivity;

    static class ActivityHandler extends Handler {
        BaseFragment fragment;

        ActivityHandler(BaseFragment activity) {
            fragment = activity;
        }

        @Override
        public void handleMessage(Message msg) {
            fragment.handMessageEvent(msg);
        }
    }

    /**
     * 事件处理
     *
     * @param msg 消息
     */
    protected void handMessageEvent(Message msg) {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(getLayoutID(), container, false);
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        initView(v);
        return v;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof BaseActivity) {
            mActivity = (BaseActivity) context;
        }
    }

    @LayoutRes
    protected abstract int getLayoutID();

    public void initView(View view) {

    }

    public void showToast(String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        if (mToast == null) {
            mToast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    //    private Dialog progressDialog;
//    private AnimationDrawable loadingAnim;
    private LoadingDialog loadingDialog;

    public void showProgressDialog() {
//        if (progressDialog == null) {
//            View view = getActivity().getLayoutInflater().inflate(R.layout.loading, null);
//            ImageView loading = (ImageView) view.findViewById(R.id.loading);
//            loadingAnim = (AnimationDrawable) loading.getDrawable();
//            progressDialog = new AlertDialog.Builder(getActivity()).setView(view).create();
//            progressDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
//            progressDialog.setCanceledOnTouchOutside(false);
//        }
//        if (loadingAnim != null && !loadingAnim.isRunning())
//            loadingAnim.start();
//        progressDialog.show();
//        return progressDialog;

        if (loadingDialog != null && loadingDialog.isVisible())
            return;
        if (loadingDialog != null) {
            loadingDialog.show(getFragmentManager(), "loadingDialog");
            return;
        }
        loadingDialog = new LoadingDialog();
        loadingDialog.show(getFragmentManager(), "loadingDialog");
    }

    public void dismissProgressDialog() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (loadingDialog != null && loadingDialog.isVisible())
                    loadingDialog.dismiss();
            }
        }, 150);
    }


    public void finish() {
        getActivity().finish();
    }

}
